package de.tum.icuproject;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import de.tum.icuproject.MainActivity.SectionsPagerAdapter;
import de.tum.icuproject.model.Player;
import de.tum.icuproject.model.Session;
import de.tum.icuproject.persistence.PlayerRepository;
import de.tum.icuproject.persistence.couchdb.PlayerRepositoryImpl;

/**
 * The Player Fragment displays the current player (if selected). 
 * Furthermore, it provides options to add, edit or delete players.
 * 
 */
public class PlayerFragment extends Fragment implements Subscriber {

	private SectionsPagerAdapter sectionsPagerAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_player,
				container, false);

		Button btnEnterId = (Button) rootView.findViewById(R.id.btnGetPlayer);
		btnEnterId.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				enterID(v);
			}
		});

		Button btnAddPlayer = (Button) rootView.findViewById(R.id.btnAddPlayer);
		btnAddPlayer.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				addPlayer(v);
			}
		});

		Button btnChangePlayer = (Button) rootView.findViewById(R.id.btnChangePlayer);
		btnChangePlayer.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				updatePlayer(v);
			}
		});

		Button btnDeletePlayer = (Button) rootView.findViewById(R.id.btnDeletePlayer);
		btnDeletePlayer.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				deletePlayer(v);
			}
		}); 

		return rootView;
	}

	/**
	 * When the fragment is started the current player is retrieved and displayed.
	 */
	@Override
	public void onStart() {
		super.onStart();

		Player currentPlayer = this.getCurrentPlayer();
		this.displayPlayer(currentPlayer);
	}

	/** Sends the ID to the database
	 * @param view
	 */
	public void enterID(View view) {
		EditText editText = (EditText) getView().findViewById(R.id.editTextPlayerId);
		String requestedID = editText.getText().toString();
		if (!requestedID.equals("")) {
			try {
				int playerId = Integer.parseInt(requestedID);
				PlayerRepository playerRepo = new PlayerRepositoryImpl(this);
				playerRepo.findByPlayerId(playerId);
			} catch (NumberFormatException e) {
				Toast.makeText(getView().getContext(), "The ID must be an integer!", Toast.LENGTH_SHORT).show();
			}
		} else {
			Toast.makeText(getView().getContext(), "Please insert an ID!", Toast.LENGTH_SHORT).show();
		}
	}

	/** Changes to the AddPlayerFragment.
	 * @param view
	 */
	public void addPlayer(View view) {
		AddPlayerFragment addPlayerFragment = new AddPlayerFragment();
		addPlayerFragment.setSectionsPagerAdapter(sectionsPagerAdapter);
		getSectionsPagerAdapter().updatePlayerFragment(addPlayerFragment);
	}

	/** Changes to the UpdatePlayerFragment.
	 * @param view
	 */
	public void updatePlayer(View view) {
		Player player = this.getCurrentPlayer();
		// if no player has been retrieved
		if (player == null) {
			Toast.makeText(getView().getContext(), "Please select a player first!", Toast.LENGTH_SHORT).show();
		}
		else {
			// Pass to update player fragment
			UpdatePlayerFragment updatePlayerFragment = new UpdatePlayerFragment();
			updatePlayerFragment.setSectionsPagerAdapter(sectionsPagerAdapter);
			getSectionsPagerAdapter().updatePlayerFragment(updatePlayerFragment);
		}
	}

	/** Changes to the DeletePlayerFragment.
	 * @param view
	 */
	public void deletePlayer(View view) {

		Player player = this.getCurrentPlayer();
		// if no player has been retrieved
		if (player == null) {
			Toast.makeText(getView().getContext(), "Please select a player first!", Toast.LENGTH_SHORT).show();
		}
		else {
			DeletePlayerFragment deletePlayerFragment = new DeletePlayerFragment();
			deletePlayerFragment.setSectionsPagerAdapter(sectionsPagerAdapter);
			getSectionsPagerAdapter().updatePlayerFragment(deletePlayerFragment);
		}
	}

	/**
	 * Displays the player in the PlayerFragment.
	 * @param player
	 */
	private void displayPlayer(Player player) {
		TextView textViewFirstName = (TextView) getView().findViewById(R.id.textViewPlayerFirstNameOut);
		TextView textViewLastName = (TextView) getView().findViewById(R.id.textViewPlayerLastNameOut);
		TextView textViewDateOfBirth = (TextView) getView().findViewById(R.id.textViewPlayerBirthdayOut);
		TextView textViewWeight = (TextView) getView().findViewById(R.id.textViewPlayerWeightOut);
		TextView textViewHeight = (TextView) getView().findViewById(R.id.textViewPlayerHeightOut);
		TextView textViewSex = (TextView) getView().findViewById(R.id.textViewPlayerSexOut);

		if (player != null) {
			textViewFirstName.setText(player.getFirstName());
			textViewLastName.setText(player.getLastName());
			textViewDateOfBirth.setText(player.getBirthDate());
			textViewWeight.setText(String.valueOf(player.getWeight()));
			textViewHeight.setText(String.valueOf(player.getHeight()));
			String sex = "Female";
			if (player.isMale()) {
				sex = "Male";
			}
			textViewSex.setText(sex);
		}
		else {
			textViewFirstName.setText("");
			textViewLastName.setText("");
			textViewDateOfBirth.setText("");
			textViewWeight.setText("");
			textViewHeight.setText("");
			textViewSex.setText("");
		}
	}

	/**
	 * Sets the current player.
	 * @param player
	 */
	public void setCurrentPlayer(Player player) {

		// the player is null if there is no player with the entered id
		if (player == null) {
			Toast.makeText(getActivity(), "There is no player with the entered id.", Toast.LENGTH_SHORT).show();
			return;
		}

		MainActivity mainActivity = (MainActivity) this.getActivity();
		mainActivity.setCurrentPlayer(player);

		this.displayPlayer(player);
	}

	@Override
	public void confirmInsertion(boolean confirm) {
	}

	@Override
	public void confirmUpdate(boolean confirm) {
	}

	@Override
	public void confirmDeletion(boolean confirm) {
	}

	@Override
	public void setPlayerId(int playerId) {
	}

	@Override
	public void setSessionId(int sessionId) {
	}

	public SectionsPagerAdapter getSectionsPagerAdapter() {
		return sectionsPagerAdapter;
	}

	public void setSectionsPagerAdapter(SectionsPagerAdapter sectionsPagerAdapter) {
		this.sectionsPagerAdapter = sectionsPagerAdapter;
	}


	@Override
	public void setCurrentSession(Session session) {
	}

	@Override
	public Player getCurrentPlayer() {
		MainActivity mainActivity = (MainActivity) getActivity();
		return mainActivity.getCurrentPlayer();
	}

}
